/**
 * @file:          Logger.c
 * @brief:
 * @details:
 * @author:        wjh
 * @date created:  2023.09.05
 * @version:       1.0.0
 * @par copyright (c):
 *
 * @par history (desc):
 *   version:1.0.0, wjh, 2023.09.05,14:31:45
 */

/* Include Files **************************************************************/
#include "Logger.h"
#include "stdlib.h"

/* Global Variable Define *****************************************************/

/* Function Define ************************************************************/

void Logger_Init(struct Logger *self)
{
    // self->Run = Logger_Run;
    // self->RegisterVariableToTable = Logger_RegisterVariableToTable;

    self->send_buff[0] = self->param_data_head;
    self->send_buff[1] = self->data_nums;
}

void Logger_Run(struct Logger *self)
{
    if (self->trigger == 1)
    {
        if (self->tick == 0)
        {
            if (self->pack_index < self->param_n_pack_to_send)
            {
                self->pack_index++;

                self->check_sum = self->send_buff[0] + self->send_buff[1];
                self->send_len = 2;
                for (uint8_t data_index = 0; data_index <= self->data_max_num; data_index++)
                {
                    if (self->data_table[data_index].addr == NULL)
                    {
                        continue;
                    }

                    LogDataUnit dat = self->data_table[data_index];
                    DataTypeTranformer dtt;

                    if (strcmp(dat.type, "uint16_t") == 0)
                    {
                        dtt.u16[0] = *(uint16_t *)(dat.addr);
                        self->send_buff[self->send_len++] = dtt.s8[0];
                        self->send_buff[self->send_len++] = dtt.s8[1];
                        self->check_sum += dtt.s8[0];
                        self->check_sum += dtt.s8[1];
                    }
                    else if (strcmp(dat.type, "int16_t") == 0)
                    {
                        dtt.s16[0] = *(int16_t *)(dat.addr);
                        self->send_buff[self->send_len++] = dtt.s8[0];
                        self->send_buff[self->send_len++] = dtt.s8[1];
                        self->check_sum += dtt.s8[0];
                        self->check_sum += dtt.s8[1];
                    }
                    else if (strcmp(dat.type, "uint32_t") == 0)
                    {
                        dtt.u32 = *(uint32_t *)(dat.addr);
                        self->send_buff[self->send_len++] = dtt.s8[0];
                        self->send_buff[self->send_len++] = dtt.s8[1];
                        self->send_buff[self->send_len++] = dtt.s8[2];
                        self->send_buff[self->send_len++] = dtt.s8[3];
                        self->check_sum += dtt.s8[0];
                        self->check_sum += dtt.s8[1];
                        self->check_sum += dtt.s8[2];
                        self->check_sum += dtt.s8[3];
                    }
                    else if (strcmp(dat.type, "int32_t") == 0)
                    {
                        dtt.s32 = *(int32_t *)(dat.addr);
                        self->send_buff[self->send_len++] = dtt.s8[0];
                        self->send_buff[self->send_len++] = dtt.s8[1];
                        self->send_buff[self->send_len++] = dtt.s8[2];
                        self->send_buff[self->send_len++] = dtt.s8[3];
                        self->check_sum += dtt.s8[0];
                        self->check_sum += dtt.s8[1];
                        self->check_sum += dtt.s8[2];
                        self->check_sum += dtt.s8[3];
                    }
                    else if (strcmp(dat.type, "float") == 0)
                    {
                        dtt.f32 = *(float *)(dat.addr);
                        self->send_buff[self->send_len++] = dtt.s8[0];
                        self->send_buff[self->send_len++] = dtt.s8[1];
                        self->send_buff[self->send_len++] = dtt.s8[2];
                        self->send_buff[self->send_len++] = dtt.s8[3];
                        self->check_sum += dtt.s8[0];
                        self->check_sum += dtt.s8[1];
                        self->check_sum += dtt.s8[2];
                        self->check_sum += dtt.s8[3];
                    }
                }
                self->send_buff[self->send_len++] = self->check_sum;

                self->updated = 1;

                self->mcu->UartDMASend(self->send_buff, self->send_len);

            }
        }
        if (self->pack_index >= self->param_n_pack_to_send && self->tick == self->param_sample_interval_n-1)
        {
            if (self->param_continuous_send == 0)
            {
                self->trigger = 0;
            }
            self->pack_index = 0;
        }
        // DBprintf("%d\r\n", self->tick);
        self->tick = (self->tick + 1) % self->param_sample_interval_n;
    }
}

void Logger_RegisterVariableToTable(struct Logger *self, uint8_t data_num, void *data_addr, uint8_t data_bytes, char *data_type, char *name)
{
    self->data_table[data_num].addr = data_addr;
    self->data_table[data_num].bytes = data_bytes;
    self->data_table[data_num].type = data_type;
    self->data_table[data_num].name = name;
    self->data_nums++;
    self->data_total_bytes += data_bytes;
    if (data_num > self->data_max_num)
    {
        self->data_max_num = data_num;
    }
}